mini.parse();

function GoodsBrandFormHandler() {
};

GoodsBrandFormHandler.instance = new GoodsBrandFormHandler();

//获得全局的商品品牌表单对象
GoodsBrandFormHandler.goodsBrandForm = new mini.Form("goodsBrandForm");
GoodsBrandFormHandler.formData = {};
//全局id
GoodsBrandFormHandler.__bgbId = null;

// 商品品牌状态数据
GoodsBrandFormHandler.bgbStateData = [
	{text: "有效",value: 1},
	{text: "无效",value: 0}
];
mini.get("bgbState").setData(GoodsBrandFormHandler.bgbStateData);

/**
 * 保存（添加或编辑）数据的方法
 */
GoodsBrandFormHandler.save = function(){
	var goodsBrandData = GoodsBrandFormHandler.goodsBrandForm.getData();
	goodsBrandData.bgbId = GoodsBrandFormHandler.formData.bgbId;
	
    GoodsBrandFormHandler.goodsBrandForm.validate();
    if (GoodsBrandFormHandler.goodsBrandForm.isValid() == false) return;
    
    var json = mini.encode(goodsBrandData);
    $.ajax({
        url: __root + "/saveOrUpdateGoodsBrand.do",
        type: "POST",
        data: {
        	goodsBrandInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		GoodsBrandFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示",jqXHR.responseText, function(){
        		GoodsBrandFormHandler.closeCurrentWin();
        	});
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
GoodsBrandFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        GoodsBrandFormHandler.formData.bgbId = data.bgbId;
        //根据id获取对应的商品品牌信息
        $.ajax({ 
            url: __root + "/getGoodsBrandInfoById.do?bgbId=" + data.bgbId,
            cache: false,
            dataType: 'json',
            success: function (text) {
            	//设置全局id
            	GoodsBrandFormHandler.__bgbId = data.bgbId;
            	//设置form表单
                GoodsBrandFormHandler.goodsBrandForm.setData(text);
                if(text.bgbLogo != null && text.bgbLogo != ""){
                	//为浏览框设置值
                	mini.get("upload_bgbLogo").setText(text.bgbLogo);
                	mini.get("upload_bgbLogo").setWidth(184);
                	
                	$("#imgViewer").empty();
					
					var iframe = "<iframe src='"+ __root + "/imgViewerContorller.do?basePath=" + __realpath + __brandImgPath + "&newImgPath=" + text.bgbLogo + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
					$("#imgViewer").append(iframe);
					
					//显示删除图标按钮
					mini.get("delBgbLogoBtn").setVisible(true);
                }
            }
        });
    }else{
    	//默认商品品牌状态为有效
    	mini.get("bgbState").setValue(1);
    }
}

/**
 * 删除商品品牌图标
 */
GoodsBrandFormHandler.delBgbLogo = function(){
	var bgbId = GoodsBrandFormHandler.__bgbId;
	var bgbLogo = GoodsBrandFormHandler.goodsBrandForm.getData().bgbLogo;
	
	if(bgbId != null && bgbId != "" && bgbLogo != null && bgbLogo != ""){
		$.ajax({
	        url: __root + "/delGoodsBgbLogo.do",
	        type: "POST",
	        data: {
	        	bgbId: bgbId,
        		lslUser: gbOperateUser,
    			lslAddress: gbOperateIp
	        },
	        cache: false,
	        success: function (text) {
	        	 MiniuiTools.miniInfo("系统提示","删除成功！");
	        	 //清空浏览框
	        	 mini.get("upload_bgbLogo").setText(null);
	        	 //重新为隐藏表单设置值
               	 mini.get("hidden_bgbLogo").setValue(null);
               	 //清空预览框
               	 $("#imgViewer").empty();
				 
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            MiniuiTools.miniError("系统提示", "删除失败！" + jqXHR.responseText);
	        }
	    });
	}else{
		MiniuiTools.miniWarning("系统提示","没有可删除的图标资源！");
	}
	
}

/**
 * 图片上传成功并预览
 * @param {} e
 */
GoodsBrandFormHandler.showBgbLogo = function(e){
	MiniuiTools.miniInfo("系统提示","文件上传成功！");
	var fdata = mini.decode(e.serverData);
	var newPath = fdata.imgPath;
	
	$("#imgViewer").empty();
	
	var iframe = "<iframe src='"+ __root +"/imgViewerContorller.do?basePath=" + __realpath+ __brandImgPath + "&newImgPath=" + newPath + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
	$("#imgViewer").append(iframe);
	
	mini.get("hidden_bgbLogo").setValue(newPath);
}

/**
 * 关闭窗口
 * @param {} action
 */
GoodsBrandFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
    GoodsBrandFormHandler.__bgbId = null;
}

GoodsBrandFormHandler.closeMiniWin = function(){
	GoodsBrandFormHandler.closeCurrentWin("cancel");
	GoodsBrandFormHandler.__bgbId = null;
}